import { Api } from './api';

export class ApiBase {
    /**
     *
     * @param namespace
     * @param api {ApiObject}
     */
    constructor(namespace, api = undefined) {
        this._namespace = namespace;
        if (typeof api === 'string') {
            this.api = Api.module(api);
        } else {
            this.api = api || Api;
        }
    }

    get Namespace() {
        return this._namespace;
    }

    addAsync(model) {
        return this.api.post(this._namespace, { model });
    }

    updateAsync(model) {
        return this.api.put(this._namespace, { model });
    }

    restoreAsync(id) {
        return this.api.put(`${this._namespace}/restore`, { id });
    }

    deleteAsync(id, reason, permanent) {
        return this.api.delete(this._namespace, { id, delete_reason: reason, permanent });
    }

    getAsync(id) {
        return this.api.get(this._namespace, { id });
    }

    getPagedAsync(params) {
        return this.api.get(`${this._namespace}/pages`, params);
    }

    getAllAsync() {
        return this.api.get(`${this._namespace}/all`);
    }
}
